package com.iotimc.attendancesystem.manage.fragment;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.base.BaseFragment;
import com.iotimc.attendancesystem.dao.DoorLogEntityDao;
import com.iotimc.attendancesystem.dao.GreenDaoManager;
import com.iotimc.attendancesystem.dao.PersonEntityDao;
import com.iotimc.attendancesystem.entity.PageHelper;
import com.iotimc.attendancesystem.entity.PersonEntity;

import org.greenrobot.greendao.query.QueryBuilder;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author david
 * @date 2017/11/28
 */

public class DoorLogFragment extends BaseFragment {


    @BindView(R.id.et_manage_search)
    EditText etManageSearch;
    @BindView(R.id.bt_manage_search)
    Button btManageSearch;
    @BindView(R.id.rc_manage_work)
    RecyclerView rcManageWork;
    PageHelper mPageHelper;
    @BindView(R.id.page_num)
    TextView pageNum;
    private DoorLogEntityDao doorLogEntityDao;

    @Override
    protected int getResId() {
        return R.layout.fragment_person_door;
    }

    @OnClick(R.id.bt_manage_search)
    void search() {
        String s = String.valueOf(etManageSearch.getText());

        QueryBuilder queryBuilder = doorLogEntityDao.queryBuilder();
        queryBuilder.join(DoorLogEntityDao.Properties.PersonId, PersonEntity.class,
                PersonEntityDao.Properties.Id)
                .whereOr(PersonEntityDao.Properties.Code.like("%" + s + "%")
                        , PersonEntityDao.Properties.Name.like("%" + s + "%"));
        queryBuilder.orderDesc(DoorLogEntityDao.Properties.CreateTime);
        mPageHelper.setQueryBuilder(queryBuilder);
        mPageHelper.headPage();
        mAdapter.addData(mPageHelper.getData());
        pageNum.setText(mPageHelper.getCurrentPage() + "/" + (mPageHelper.getPageNum()==0?1:mPageHelper.getPageNum()));

        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        View focus = getActivity().getCurrentFocus();
        if (imm != null && focus != null) {
            imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


    private DoorManageAdapter mAdapter;


    @Override
    public void onStart() {
        super.onStart();
        mPageHelper = new PageHelper(10);
        doorLogEntityDao = GreenDaoManager.getInstance().getSession().getDoorLogEntityDao();

        QueryBuilder queryBuilder = doorLogEntityDao.queryBuilder();
        queryBuilder.join(DoorLogEntityDao.Properties.PersonId, PersonEntity.class,
                PersonEntityDao.Properties.Id);
        queryBuilder.orderDesc(DoorLogEntityDao.Properties.CreateTime);

        mPageHelper.setQueryBuilder(queryBuilder);
        pageNum.setText(mPageHelper.getCurrentPage() + "/" + (mPageHelper.getPageNum()==0?1:mPageHelper.getPageNum()));

        rcManageWork.setLayoutManager(new LinearLayoutManager(getActivity(), 1, false));
        mAdapter = new DoorManageAdapter(mPageHelper.getData(), getActivity());
        rcManageWork.setAdapter(mAdapter);
    }

    @OnClick({R.id.page_head, R.id.page_up, R.id.page_num, R.id.page_next, R.id.page_end})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.page_head:
                mPageHelper.headPage();
                break;
            case R.id.page_up:
                if (mPageHelper.hasPrePage()) {
                    mPageHelper.prePage();
                } else {
                    return;
                }
                break;
            case R.id.page_num:
                break;
            case R.id.page_next:
                if (mPageHelper.hasNextPage()) {
                    mPageHelper.nextPage();
                } else {
                    return;
                }
                break;
            case R.id.page_end:
                mPageHelper.lastPage();
                break;
            default:
                break;
        }

        pageNum.setText(mPageHelper.getCurrentPage() + "/" + (mPageHelper.getPageNum()==0?1:mPageHelper.getPageNum()));
        mAdapter.addData(mPageHelper.getData());
    }
}
